Row

Fires Frequency over months

Fire Duration

Acres Burned Since 2013

Row

Fatalities Count over the Years

Structures Destroyed Over the Years

Fire Incident on California Map

---
title: "California Wildfire and its impacts"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    social: [ "twitter", "facebook", "menu"]
    source_code: embed
    theme: spacelab
---


```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(readr)
library(ggplot2)
```


```{r include=FALSE}
california_fire <- read_csv("/Users/devanjal/Downloads/California_Fire_Incidents.csv")
head(california_fire)
```
```{r}
library(lubridate)
library(dplyr)
california_fire <- california_fire %>%  mutate(Started = as.Date(Started), Extinguished = as.Date(Extinguished))
```

```{r}
california_fire <- california_fire %>%  mutate(month = month(Started, label = TRUE),
         year = year(Started))
```

```{r}
california_fire <- california_fire %>%  mutate(fire_duration = Extinguished - Started)
```

```{r}
california_fire <- california_fire %>%  mutate(acres_burned_cumulative = cumsum(AcresBurned))
```


```{r include=FALSE}
#area burnt
ggplot(data = california_fire) +
  aes(x = AcresBurned) +
  geom_histogram(bins  = 30, fill="#69b3a2", color="#e9ecef", alpha=0.9) +
  theme_classic() +
  scale_x_continuous(labels = scales::comma)
#fire_duration
ggplot(data = california_fire) +
  aes(x = fire_duration) +
  geom_histogram(bins  = 30, fill="#69b3a2", color="#e9ecef", alpha=0.9) +
  theme_classic() +
  scale_x_continuous(labels = scales::comma)
```
```{r include=FALSE}
california_fire %>%  filter(fire_duration < 0)
```
```{r}
california_fire <- california_fire %>%
    filter(fire_duration >0)
```

Row
-------------------------------------

```{r}
fire_month_data <- california_fire %>%
  group_by(month) %>%
  summarize(fires = n())
```

### **Fires Frequency over months** 
```{r}
library(plotly)
b <- ggplot(data = fire_month_data) +
  aes(x = month, y = fires) +
  geom_bar(stat = "identity", fill="#69b3a2", color="#e9ecef") +
  theme(legend.position="none")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank())+
  scale_y_continuous(labels = scales::comma)
ggplotly(b)
```

###  **Fire Duration**
```{r}
#fire_duration
a <- ggplot(data = california_fire) +
  aes(x = fire_duration) +
  geom_histogram(bins  = 30, fill="#69b3a2", color="#e9ecef", alpha=0.9) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank())+
  xlab("Fire Duration in Days") +
  scale_x_continuous(labels = scales::comma) + 
  coord_cartesian(xlim = c(0, 500))
ggplotly(a)
```


```{r include=FALSE}
year_month_data <- california_fire %>%
 group_by(month, year) %>%
  summarize(fires = n())

ggplot(data = year_month_data) +
  aes(x = month, y = fires, fill= month) +
  geom_boxplot(alpha=0.5) +
  theme(legend.position="none") +
  scale_fill_brewer(palette="Dark2") +
  scale_y_continuous(labels = scales::comma)

```
### **Acres Burned Since 2013** 
```{r, fig.width=8, fig.height=8}
library(plotly)
library(hrbrthemes)
p <- california_fire %>%
  ggplot(aes (x = Started, y = acres_burned_cumulative)) +
  geom_area(fill="#69b3a2", alpha=0.2) +
  geom_line(color="#69b3a2") +
  scale_y_continuous(labels = scales::comma) +
  ylab("Acres Burned (cumulative)") +
  xlab("date") +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank())+
  theme_ipsum()
# Turn it interactive with ggplotly
p <- p + coord_cartesian(ylim = c(0, 8000000))
p <- ggplotly(p)
p

# save the widget
# library(htmlwidgets)
# saveWidget(p, file=paste0( getwd(), "/HtmlWidget/ggplotlyAreachart.html"))

```
Row
-------------------------------

```{r include=FALSE}
fatal_count <- aggregate(california_fire$Fatalities, list(california_fire$ArchiveYear), FUN=sum, na.rm=TRUE, na.action=NULL) 
fatal_count
```

### **Fatalities Count over the Years**
```{r}
fatal_count %>%
  tail(10) %>%
  ggplot( aes(x=Group.1, y=x)) +
  ylab("Fatalities Count") +
  xlab("Year")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank(), 
         axis.text=element_text(size=20),
         axis.title=element_text(size=20))+
  geom_line( color="grey") +
  geom_point(shape=21, color="black", fill="#e29f7a", size=6) +
  theme_ipsum()
```

### **Structures Destroyed Over the Years**
```{r}
library(ggthemes)
library(scales)
library(maps)
library(mapproj)

d <- ggplot(data = california_fire) +
  aes(x = year, y = StructuresDestroyed) +
  geom_bar(stat = "identity", fill="#e29f7a") +
  ylab("Structures Destroyed") +
  xlab("Year")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank(), 
         axis.text=element_text(size=10),
         axis.title=element_text(size=8))+
  scale_y_continuous(labels = scales::comma)
ggplotly(d)
```

```{r include=FALSE}
# library
library(ggplot2)
library(hrbrthemes)
# The iris data set is provided natively by R
#head(iris)
 
# basic scatter-plot
tbl2 <- california_fire %>% filter(Longitude < -115)
tbl2 <- tbl2 %>% filter(Latitude < 44)
tbl2 <- tbl2 %>% filter(Latitude > 30)
tbl2 <- tbl2 %>% filter(CanonicalUrl!='/incidents/2013/8/6/tram-fire/')
ggplot(tbl2, aes(x=Longitude, y=Latitude)) + 
   geom_point(size=2) +
    theme_ipsum()
```
```{r include=FALSE}
library(ggmap)
cal_map <- get_stamenmap(
  bbox= c(left = -125, bottom = 31.5, right = -114, top = 43),
  maptype = 'terrain',
  zoom = 5
)
```

### **Fire Incident on California Map**
```{r,fig.width=8, fig.height=8}
ggmap(cal_map)+
geom_point(data=tbl2,
             aes(x=Longitude,y=Latitude, color= 'california_wildfire'),
             size=1.5)+
  ylab("Latitude") +
  xlab("Longitude")+
  theme(legend.position="none", plot.title=element_text(size=16,face="bold"),
         axis.text=element_text(size=18),
         axis.title=element_text(size=20,face="bold"))
```


```{r  include=FALSE}
theme_map()
```


```{r include=FALSE}
WaterTenders <- california_fire$WaterTenders
hist(WaterTenders, col="sky blue",)
```